const app = getApp()
const api = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    amount: '',
    WxCard: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      amount: options.needPay
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    // 注册登录 
    api.post('user/getByMobile', {
      mobile: wx.getStorageSync('phone'),
      openid: wx.getStorageSync('user').openid,
      shopId: app.globalData.shopId,
    }).then(res => {
      console.log(res)
      // 获取用户信息
      if (res.data.success) {
        that.setData({
          WxCard: res.data.data.wxCard
        })

      }

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /* 【用户中心】 */
  goUser: function () {
    setTimeout(function () {
      wx.reLaunch({
        url: '/pages/user/index'
      })
    }, 200)
  },
  /* 用户领取卡 */
  goCard: function () {
    // this.clickMyCard()
    this.openCard()
  },
  // 领取会员卡
  clickMyCard: function () {
    var that = this
    wx.request({
      url: api.baseUrl + 'user/getSignature',
      method: 'POST',
      data: {
        cardId: app.globalData.cardId,
        shopId: app.globalData.shopId
      },
      success: function (res) {
        var cardExt = {}
        cardExt.nonce_str = res.data.data.noncestr
        cardExt.timestamp = res.data.data.timestamp
        cardExt.signature = res.data.data.signature
        wx.addCard({
          cardList: [{
            cardId: app.globalData.cardId,
            cardExt: JSON.stringify(cardExt)
          }],
          success(res) {
            that.getCardNo(res.cardList[0].code),
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/index'
                })
              }, 200)
          },
          complete: function (res) {
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/user/index'
              })
            }, 200)
        }
        })

      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  /* 解密卡号 */
  getCardNo: function (cardCode) {
    var that = this
    api.post('user/saveCardNum', {
      code: cardCode,
      mobile: wx.getStorageSync('phone'),
      shopId: app.globalData.shopId,
    }).then(res => {
      // if (res.data.success) {
      //     that.setData({
      //         cardNo: res.data.data.cardNum
      //     })
      // }
    })
  },
  openCard: function () {
    api.post('shop/getCardActivate', {
      shopId: app.globalData.shopId,
    }).then(res => {
      console.log(res)
      let obj = {}
      obj['biz'] = decodeURIComponent(res.data.data.biz);
      obj['encrypt_card_id'] = decodeURIComponent(res.data.data.encrypt_card_id);
      obj['outer_str'] = decodeURIComponent(res.data.data.outer_str)
      wx.navigateToMiniProgram({
        appId: 'wxeb490c6f9b154ef9', //固定为此 appid，不可改动
        extraData: res.data.data, // 包括 encrypt_card_id, outer_str, biz三个字段，须从 step3 中获得的链接中获取参数
        success: function (res) {
          console.log(res, 11)
        },
        fail: function (res) {
          console.log(res)
        },
        complete: function (res) {
          console.log(res, 22)
        }
      })
    })

  }
})